package com.wdl.sm.webui.yang.node;

import java.util.Arrays;
import java.util.List;

import com.nokia.taiji.yang.schema.SchemaNode;
import com.nokia.taiji.yang.schema.YANG_Leaf;
import com.nokia.taiji.yang.schema.YANG_List;
import com.nokia.taiji.yang.schema.YangType;
import com.wdl.sm.webui.yang.YANGExt;
import com.wdl.sm.webui.yang.type.NormalizedValue;

public class LeafNode extends NormalizedNode {
    NormalizedValue value;
    boolean key = false;

    public LeafNode(YANG_Leaf node) {
        super(node);

        SchemaNode parent = node.getParent();
        if (parent != null && parent instanceof YANG_List) {
            String[] keys = ((YANG_List) parent).getKeys();
            if (keys != null && keys.length > 0) {
                List<String> keyList = Arrays.asList(keys);
                key = keyList.contains(node.getID());
            }
        }

        YangType nodeType = node.getType();
        value = YANGExt.buildWebuiYangValue(nodeType);
        if (value == null) {
            LOG.info("There's no value found for leaf node: " + node.getSNI());
            return;
        }

        String unit = node.getUnits();
        if (unit != null) {
            value.setUnits(unit);
        }

        String myDefault = node.getDefaultValue();
        if (myDefault != null) {
            value.setDefaultValue(node.getDefaultValue());
        }
    }

    public NormalizedValue getValue() {
        return value;
    }

    public void setValue(NormalizedValue value) {
        this.value = value;
    }

    public boolean isKey() {
        return key;
    }
}
